﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using ListForms;
using DevExpress.XtraEditors;

namespace ListsForm
{
    public partial class FrmEditStaff : FrmBaseEditForm
    {
        public FrmEditStaff( Lists.Staff staff)
        {
            InitializeComponent();
            if (staff == null)
            {
                _EditState = EditState.CreatingNew;
                bindingSource.AddNew();
                this.Text = "New Staff";
            }
            else
            {
                _EditState = EditState.Modifying;
                bindingSource.Add(DB.Staffs.FirstOrDefault(p => p.StaffID == staff.StaffID));
                this.Text = "Edit Staff";
            }
            PositionIDLookUpEdit.Properties.DataSource = DB.Positions.ToList();
            StaffDOBDateEdit.Properties.MinValue = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
        }

        protected override void CheckFullInfo()
        {
            Lists.Staff staff = (Lists.Staff)bindingSource.Current;
            if (string.IsNullOrEmpty(staff.StaffNo))
            {
                StaffNoTextEdit.Focus();
                throw new Exception("Staff ID not allow null");
            }
            if (string.IsNullOrEmpty(staff.StaffName))
            {
                StaffNameTextEdit.Focus();
                throw new Exception("Staff name not allow null");
            }
            if (staff.StaffDOB==null)
            {
                StaffDOBDateEdit.Focus();
                throw new Exception("Staff DOB not allow null");
            }
            if ((DateTime.Now - staff.StaffDOB.Value).Days / 365 < 18)
            {
                StaffDOBDateEdit.Focus();
                throw new Exception("Staff year old cannot less than 18");
            }
            if ((DateTime.Now - staff.StaffDOB.Value).Days / 365 > 100)
            {
                StaffDOBDateEdit.Focus();
                throw new Exception("Staff year old cannot more than 100");
            }
            if (string.IsNullOrEmpty(staff.StaffAddress))
            {
                StaffAddressMemoEdit.Focus();
                throw new Exception("Staff address not allow null");
            }
            if (string.IsNullOrEmpty(staff.StaffTel))
            {
                StaffTelTextEdit.Focus();
                throw new Exception("Staff tel not allow null");
            }
            Regex regexnumber = new Regex(@"[\d]");
            if (!regexnumber.IsMatch(staff.StaffTel))
            {
                StaffTelTextEdit.Focus();
                throw new Exception("Staff tel number only");
            }
            if (string.IsNullOrEmpty(staff.StaffEmail))
            {
                StaffEmailTextEdit.Focus();
                throw new Exception("Staff email not allow null");
            }
            if (string.IsNullOrEmpty(staff.StaffIdentityCard))
            {
                StaffIdentityCardTextEdit.Focus();
                throw new Exception("Staff identity card not allow null");
            }
            if (!regexnumber.IsMatch(staff.StaffIdentityCard))
            {
                StaffIdentityCardTextEdit.Focus();
                throw new Exception("Staff identity card number only");
            }
            if (PositionIDLookUpEdit.EditValue==DBNull.Value)
            {
                PositionIDLookUpEdit.Focus();
                throw new Exception("Staff position not allow null");
            }
        }

        protected override void SaveAndClose()
        {
            Lists.Staff staff = (Lists.Staff)bindingSource.Current;
            if (_EditState == EditState.CreatingNew)
                DB.Staffs.InsertOnSubmit(staff);
            DB.SubmitChanges();
            this.Close();
        }
    }
}
